from settings import web_type
from util.DataQuery.DbTools import DbTools
from util.DataQuery.hzflowOperation import hzflowOperation
from util.g import g
from util.inform.sendNote import AliyunSMS
from util.selenium.webdriver.common.by import By

from base.BaseMethod import BM

# 点击 我要办理 按钮
class ToTransaction():
    # 新增的元素

    weishiming_loc = (By.CSS_SELECTOR, "#weishiming")
    # 关闭企业基本信息按钮
    close_btn_loc = (By.CSS_SELECTOR, ".layui-layer-setwin a")
    # 在线办事按钮   选中的按钮 class="menu-item-active"
    on_line_loc = (By.XPATH, "//a[contains(string(),'在线办事')]")
    # 我要办理 按钮 选中的按钮 class="onclick_div"
    transact_btn_loc = (By.XPATH, "//div[@class='yhfw_left']/div[contains(string(),'我要办理')]")
    # 已申请 选框
    apply_loc = (By.XPATH, "//label[contains(string(),'已申请')]")
    apply1_loc = (By.XPATH, "//label[contains(string(),'已授权')]")
    # 未申请 选框
    un_apply_loc = (By.XPATH, "//label[contains(string(),'未申请')]")
    un_apply1_loc = (By.XPATH, "//label[contains(string(),'未授权')]")

    enter_loc = (By.XPATH, "//div[@class='wybl-tr'][contains(string(), '高新技术企业认定')]/div[3]/a")

    tip_loc = (By.CSS_SELECTOR, ".layui-layer-content")



    #  原来页面的元素
    iframe_loc = (By.CSS_SELECTOR, '#main #right iframe')

    transaction_btn_loc = (By.CSS_SELECTOR, '.serverList div:nth-of-type(2) div button.blue')
    main_frame_loc = (By.CSS_SELECTOR, '#main #right iframe')

    hint_window_loc = (By.CSS_SELECTOR, 'div.TSwindow')

    hint_window_accept_btn_loc = (By.CSS_SELECTOR, 'div.TSwindow div.btnBox a:nth-child(1)')

    hint_window_cancel_btn_loc = (By.CSS_SELECTOR, 'div.TSwindow div.btnBox a:nth-child(2)')

    # 写代码时方便提示, 基本方法完成后删除
    # driver = driver

    def __init__(self, driver, status):
        self.driver = driver
        self.status = status

    def click_transaction_btn(self):

        self.driver.find_element(*self.transaction_btn_loc).click()

    def to_transaction(self):
        # ##############  infasible  如果登录之后提示弹框出现， 强制等待5秒，然后直接跳转
        # if self.driver.find_element(*self.hint_window_loc).is_displayed():
            ## 当提示框出现时，直接点击确认按钮
        # try:
        #     self.driver.find_element(*self.hint_window_cancel_btn_loc).click()
        # except Exception as err:
        #     print(err)
        if BM.isElementExist(element=self.close_btn_loc, driver=self.driver):
            BM.click(driver=self.driver, ele_loc=self.close_btn_loc)
        on_line = BM.get_ele(driver=self.driver, ele_loc=self.on_line_loc, time=3)
        if "menu-item-active" not in on_line.get_attribute("class"):
            BM.click(driver=self.driver, ele_loc=self.on_line_loc)

        transact_btn = BM.get_ele(driver=self.driver, ele_loc=self.transact_btn_loc)
        if "onclick_div" not in transact_btn.get_attribute("class"):
            BM.click(driver=self.driver, ele_loc=self.transact_btn_loc)

        BM.click(driver=self.driver, ele_loc=self.apply_loc if g.login == 1 else self.apply1_loc)
        if "高新技术企业认定" in self.driver.page_source:
            BM.click(driver=self.driver, ele_loc=self.enter_loc)
        if BM.isElementExist(element=self.tip_loc, driver=self.driver):
            tip = self.driver.find_element(*self.tip_loc).text
            if u"没有实名认证" in tip:
                status = self.status
                map = {"ul_status": -1, "c_idstatus": 1}
                DbTools.update_entrestatus_fields(map, co_id=g.co_id, year=g.year)
                hzflowOperation.update_host_process(obj=hzflowOperation.get_log(g.co_id, g.year), mark_info="")
                AliyunSMS().send_single(
                    phone=status.get("phone"), template="SMS_192820696",
                    params={"resinfo": "{}, 企业国网账号未实名认证".format(status.get("entname"))},
                    fb_time=status.get("fb_time"), res_code=web_type)
                self.driver.quit()
            return True
        return False

        # "高新技术企业认定管理工作网"

        # BM.wait(driver=self.driver, ele_loc=self.iframe_loc)
        # if len(self.driver.window_handles) > 1:
        #     return
        # BM.switch_to_frame(driver=self.driver, ele_loc=self.main_frame_loc)
        # # time.sleep(2)
        # if self.driver.title == u'高新技术企业认定管理工作网':
        #     return
        # BM.click(driver=self.driver, ele_loc=self.transaction_btn_loc)
        # self.driver.refresh()
